<template>
  <div class="v-container">
    <v-header title="个人中心">
      <span class="back" @click="goBack">
        <i class="el-icon-arrow-left"></i>返回
      </span>
    </v-header>
    <div class="v-main">
      <div class="userbox">
        <img class="userimg fl" src="@/assets/images/avatar.png" width="80" alt="用户头像">
        <div class="usermsg">
          <table>
            <tr>
              <td class="usermsg-tit">手机号：</td>
              <td>{{userInfo.telphone | telphone}}</td>
            </tr>
            <tr>
              <td class="usermsg-tit">卡号：</td>
              <td>{{userInfo.userName | userName}}</td>
            </tr>
          </table>
        </div>
      </div>
      <div class="logout">
        <mt-button class="my-button hot-button round" @click="logout">退出登录</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import vHeader from '@/components/Header.vue';
import { Resource } from '@/services/resource';
import Cookies from 'js-cookie';
import { staticPath } from '@/env';
import { MessageBox } from 'mint-ui';

export default {
  name: 'activity',
  components: {
    vHeader
  },
  data() {
    return {
      userInfo: {
        telphone: '',
        userName: ''
      }
    };
  },
  mounted() {
    this.getUserInfo();
  },
  methods: {
    // 返回上一页
    goBack() {
      this.$router.go(-1);
    },
    // 获取用户信息
    getUserInfo() {
      Resource.getUserInfo.post({}, {}).then(res => {
        let { telphone, userName } = res.user;
        Object.assign(this.userInfo, { telphone, userName });
      });
    },
    // 退出登录
    logout() {
      const msgBox = MessageBox({
        title: '提示',
        message: '确定退出?',
        showCancelButton: true
      });
      msgBox.then(action => {
        if (action === 'confirm') {
          Resource.logOut.post({}, {}).then(res => {
            console.log(res);
            Cookies.remove('token', { path: staticPath });
            this.$store.dispatch('logout');
            this.$router.push('login');
          });
        }
      });
    }
  },
  filters: {
    telphone: function(value) {
      return value.substring(0, 3) + '****' + value.substring(7, 11);
    },
    userName: function(value) {
      let tmp = value.substring(4, value.length - 4);
      let tmp2 = '';
      for (let i = 0; i < tmp.length; i++) {
        tmp2 += '*';
      }
      value = value.substring(0, 4) + tmp2 + value.substring(value.length - 4, value.length);
      return value;
    }
  }
};
</script>

<style lang="less" scoped>
@import url('./personal.less');
</style>
